package leetcode.dp;

import java.util.Scanner;

public class NQueen {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(process(n));
    }
    public static int process(int n){
        if (n <1 ){
            return 0;
        }
        int[] record = new int[n];
        return process(n,0,record);
    }
    public static int process(int n,int m,int[] record){
        if (n == m){
            return 1;
        }
        int res = 0;
        for (int i = 0; i <n; i++) {
            if (isValid(record,m,i)){
                record[m] = i;
                res += process(n,m+1,record);
            }
        }
        return res;
    }

    public static boolean isValid(int[] record,int m, int i){
        for (int j = 0; j < m; j++) {
            if (i == record[j] || Math.abs(record[j]-i) == Math.abs(m-j)){
                return false;
            }
        }
        return true;
    }


}
